import * as commonService from '../services/CommonCRUD';
import * as menuService from '../services/menus';

export default {
    namespace: 'popedom',
    state: {
        roleDatas: [],
        permTreeNodes: [],
        UserTreeNodes: [],
        permTreeCheckedKeys: [],
        userTreeCheckedKeys: [],
        permUserCheckedKeys: [],
        roleTreeCheckedKeys: []
    },
    reducers: {
        saveRoles(state, { payload: { roleDatas } }) {
            return { ...state, roleDatas };
        },
        savePermTree(state, { payload: { permTreeNodes } }) {
            return { ...state, permTreeNodes };
        },
        saveUserTree(state, { payload: { UserTreeNodes } }) {
            return { ...state, UserTreeNodes };
        },
        savePermTreeCheckedKeys(state, { payload: { permTreeCheckedKeys } }) {
            return { ...state, permTreeCheckedKeys };
        },
        saveUserTreeCheckedKeys(state, { payload: { userTreeCheckedKeys } }) {
            return { ...state, userTreeCheckedKeys };
        },
        savePermUserCheckedKeys(state, { payload: { permUserCheckedKeys } }) {
            return { ...state, permUserCheckedKeys };
        },
        saveRoleTreeCheckedKeys(state, { payload: { roleTreeCheckedKeys } }) {
            return { ...state, roleTreeCheckedKeys };
        },
    },
    effects: {
        *fetchRoles({ payload: { daoName, permID } }, { select, call, put }) {
            const menus = yield select(state => state.menus);
            const { popedomTypes } = menus;
            const pTypes = popedomTypes[permID];

            let where = "";
            if (pTypes.indexOf("4") == -1 && pTypes.indexOf("O") !== -1) {
                where = "RoleName like '二级%'"
            }
            const listData = yield call(commonService.fetchAll, { daoName, where });
            if (listData) {
                yield put({
                    type: 'saveRoles',
                    payload: {
                        roleDatas: listData
                    },
                });
            }
        },
        *fetchPermTree({ payload: { permID } }, { call, put }) {
            const listData = yield call(menuService.menuTreeNodeFetch, { permID });
            if (listData) {
                yield put({
                    type: 'savePermTree',
                    payload: {
                        permTreeNodes: listData
                    },
                });
            }
        },
        *fetchUserTree({ payload: { permID } }, { call, put }) {
            const listData = yield call(menuService.menuUserTreeNodeFetch, { permID });
            if (listData) {
                yield put({
                    type: 'saveUserTree',
                    payload: {
                        UserTreeNodes: listData
                    },
                });
            }
        },
        *fetchPermTreeCheckedKeys({ payload: { roleID } }, { call, put }) {
            const listData = yield call(menuService.CheckedRolePermsFetch, { roleID });
            if (listData) {
                yield put({
                    type: 'savePermTreeCheckedKeys',
                    payload: {
                        permTreeCheckedKeys: listData
                    },
                });
            }
        },
        *fetchUserTreeCheckedKeys({ payload: { roleID } }, { call, put }) {
            const listData = yield call(menuService.CheckedRoleUsersFetch, { roleID });
            if (listData) {
                yield put({
                    type: 'saveUserTreeCheckedKeys',
                    payload: {
                        userTreeCheckedKeys: listData
                    },
                });
            }
        },
        *fetchPermUserCheckedKeys({ payload: { userID } }, { call, put }) {
            const listData = yield call(menuService.CheckedUserPermsFetch, { userID });
            if (listData) {
                yield put({
                    type: 'savePermUserCheckedKeys',
                    payload: {
                        permUserCheckedKeys: listData
                    },
                });
            }
        },
        *fetchRoleTreeCheckedKeys({ payload: { userID } }, { call, put }) {
            const listData = yield call(menuService.CheckedUserRolesFetch, { userID });
            if (listData) {
                yield put({
                    type: 'saveRoleTreeCheckedKeys',
                    payload: {
                        roleTreeCheckedKeys: listData
                    },
                });
            }
        }
    }
}